package monitor.view.bean;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import monitor.model.dao.CarDAO;
import monitor.model.entity.Car;

/**
 * Used to manage all cars of current user.
 */
@ManagedBean(name = "carsController")
@ViewScoped
public class CarsController extends Base {
	
	private static final long serialVersionUID = 3453894125859300525L;

	//all cars of current user
	private List<Car> availableCars;
	
	//this field may be used for deletion or data show
	private Car selectedCar;
	
	//object that represents newly created car
	private Car newCar = new Car();
	
	//set to true if car is created or deleted
	boolean forceCarsRetrievement = true;
	
	public void addCar() {
		CarDAO.getInstance().insert(this.newCar);
		this.newCar = new Car();
		forceCarsRetrievement = true;
	}
	
	public List<Car> getAvailableCars() {
		//query to DB is forced if there is new or deleted car.
		if(forceCarsRetrievement) {
			this.availableCars = CarDAO.getInstance().retrieveAllCars();
			forceCarsRetrievement = false;
		}
		return this.availableCars;
	}
	
	public void deleteSelectedCar() {
		CarDAO.getInstance().deleteCar(selectedCar);
		forceCarsRetrievement = true;
	}

	public Car getNewCar() {
		return newCar;
	}

	public void setNewCar(Car car) {
		this.newCar = car;
	}

	public Car getSelectedCar() {
		return selectedCar;
	}

	public void setSelectedCar(Car selectedCar) {
		this.selectedCar = selectedCar;
	}

}
